const Koa = require('koa');
const Router = require('@koa/router');
const serve = require('koa-static');
const bodyParser = require('koa-bodyparser');
const authRoutes = require('./routes/auth');
const todoRoutes = require('./routes/todos');

const app = new Koa();
const router = new Router();
const PORT = 8080;

// 使用 bodyparser 解析 POST 请求的 JSON 数据
app.use(bodyParser());

// 提供静态文件服务，确保 public 文件夹下的文件可以被访问
app.use(serve(__dirname + '/public'));

// 注册用户认证和待办事项路由
router.use('/api/auth', authRoutes.routes());
router.use('/api/todos', todoRoutes.routes());

// 应用路由中间件
app.use(router.routes()).use(router.allowedMethods());

// 启动服务器
app.listen(PORT, () => {
  console.log(`Server is running at http://localhost:${PORT}`);
});
